/**
 * Created by zycool on 2016/11/3.
 * 高程 - 管道基础数据 - 基础数据
 */

;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        mileType: 'float',
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        mileType: 'float',
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment, 'beginSignId': $begin}
    });
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });

    var initData = {
        pipeLineId: $('#JQ_pipeId').val(),
        pipeLineName: $('#JQ_pipeName').val(),
        pipeSegmentId: $('#JQ_segmentId').val(),
        pipeSegmentName: $('#JQ_segmentName').val(),
        beginId: $('#JQ_beginId').val(),
        beginName: $('#JQ_beginName').val(),
        beginMile: $('#JQ_beginMileage').val(),
        endId: $('#JQ_endId').val(),
        endName: $('#JQ_endName').val(),
        endMile: $('#JQ_endMileage').val()
    };

    if (initData) {
        $pipeline.fit(initData.pipeLineId);
        $segment.asset([{id: initData.pipeSegmentId, text: initData.pipeSegmentName}]);
        $begin.asset([{id: initData.beginId, text: initData.beginName, mile: initData.beginMile}]);
        $end.asset([{id: initData.endId, text: initData.endName, mile: initData.endMile}]);
    }

    var filter = {
        getSearch: function () {
            var data = {};
            data.mode = true;
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                } else {
                    $pipeline.element.TIP({content: '管道名称不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            data.pipeLineName = function () {
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            data.startMileage = function () {
                var v = $begin.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            data.endMileage = function () {
                var v = $end.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();

            return data;
        },
        getExport: function(){
            return filter.getSearch();
        }
    };

    // 查询
    $('#JQ_search_gaoceng').click(function () {
        var param = filter.getSearch();
        if (param.mode) {
            DyyBase.ajax(baseDataApi.url_pipe_gaoceng(), param, function (dataset) {
                if (dataset.bool) {
                    var gao = d3Charts.formart(dataset.result, {x: 'mileage', y: 'z'});
                    if (gao.length > 0) {
                        d3Charts.dogLeg({
                            container: 'JQ_pipe_gaoceng',
                            data: gao,
                            color: ['#04adee'],
                            xAxis: {
                                ticks: Infinity
                            },
                            yAxis: {
                                ticks: Infinity
                            },
                            layout: {
                                xtag: '绝对距离(m)',
                                ytag: '高程(m)',
                                margin: {
                                    left: 30,
                                    right: 30,
                                    top: 30,
                                    bottom: 40
                                },
                                xgrid: true,
                                ygrid: true
                            },
                            path: {
                                show: true,
                                type: 'area',
                                color: ['#04adee', '#04adee']
                            },
                            pointText: {
                                startShow: true,
                                stopShow: true
                            },
                            tip: {
                                xtag: '绝对距离(m)',
                                ytag: '高程(m)'
                            }
                        });
                    } else {
                        $('#JQ_pipe_gaoceng').trigger('clear');
                    }
                }
            });
        }
    });

    // 执行绘制
    if (initData) {
        $('#JQ_search_gaoceng').trigger('click');
    }

    // 导出
    $('#JQ_statistics_export').click(function () {
        var param = (filter.getExport instanceof Function) ? filter.getExport() : filter.getExport;
        var $this = $(this);
        DyyBase.isLogin(function(){
            $this.EXPT({
                type: 'name',
                name: param.pipeLineName + '高程信息',
                yesBtnAct: function(callback, fname){
                    callback();
                    canvasPdf.exportHtmlPdf({
                        dom: $('#JQ_pdf_area'),
                        server: DyyBase.root_path + '/comm/downImgPdf.json',
                        param: {
                            fileName: fname,
                            name: fname
                        }
                    });
                }
            });
        });
    });

    // 返回
    $('#JQ_statistics_goback').click(function () {
        window.history.go(-1);
    });

})(jQuery, window, document);
